import 'package:client_flutter_android/http/box.dart';
import 'package:flutter/material.dart';

import 'bean/cabinet.dart';
import 'package:collection/collection.dart';
class DoorModel extends ChangeNotifier {
  List<Cabinet> _boxs = [];
  List<Cabinet> get boxs => _boxs;
  getBoxs() async {
    try {
      final result = await Box.getBoxs();
      if (result.isNotEmpty) {
        _boxs = result;
        notifyListeners();
        debugPrint('_boxs.length******${_boxs.length}');
      } else {
        debugPrint('没有加载的锁数据信息');

        throw '加载柜门锁信息失败';
      }
    } catch (e) {
      debugPrint('加载柜门锁信息异常$e');
    }
  }

  setBox(id,czh, occupied) {
    debugPrint('_boxs.length:${_boxs.length}*****');
    Cabinet? box = _boxs.firstWhereOrNull((box) {
      if (box.id == id) {
        return true;
      }
      return false;
    });
    box.occupied = occupied;
    box.czh=czh;
    notifyListeners();
      // _boxs[id - 1].occupied = occupied;
    // notifyListeners();
  }

  DoorModel() {
    getBoxs();
  }
}
